import os, urllib, urllib2, urlparse
import libxml2dom
#from oauth import OAuthRequest
import oauth.oauth as oauth
#from oauth.signature_method.hmac_sha1 import OAuthSignatureMethod_HMAC_SHA1
consumer_key = 'tcq3zq6ekxkzptucsybrkkaf'
consumer_secret = 'VK2cVReGkB'
userid = 'T1BKsCuV.v4xSgMBlLo3aDoeLU9a6LPW6THI5wb3ysup0-'
movie_url = 'http://api.netflix.com/catalog/titles/movies/'
test_id = '60022989'
auth_data = {'request':{'key':'jydzejqhj3rus7b4r6y3m886','secret':'KzZZxbnREq9g'}, 'access':{'key':'BQAJAAEDECPaZ7zKMT-DlLGKufMmNJ0whtSGizzgxMbrSXdbBu3JUFyo6VU1eoQkro5boaNDs8FIUcFl1PUaof8w2F12W8-o','secret':'6u7QpNm4NnXN'}}

def compile_tag_file():
	movie_ids = []
	for file in os.listdir(reduced_training_set_dir):
		movie_ids.append(int(file[3:-4]))
	read_file = open("movie_titles_with_ids.txt",'r')
	write_file = open("reduced_movie_list.txt", 'w')
	for line in read_file:
		if movie_ids.__contains__(int(line.split('\t')[0])):
			#stuff goes here
			write_file.write(line)
	read_file.close()
	write_file.close()
	
def get_auth_request():
	request_token_url = 'http://api.netflix.com/oauth/request_token'
	#consumer = {'oauth_token': 'tcq3zq6ekxkzptucsybrkkaf', 'oauth_token_secret': 'VK2cVReGkB'}
	consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)
	request = oauth.OAuthRequest.from_consumer_and_token(consumer,  http_url=request_token_url)
	request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), consumer, None)
	request_url = request.to_url()
	print request_url
	temp_response = urllib.urlopen(request_url).read()
	print temp_response
	requestToken = oauth.OAuthToken.from_string(temp_response)
	#token_response_dict = urlparse.parse_qs(temp_response)  #get response, parse as query string.  returns dict
	#print token_response_dict
	print "\n\n"
	params = {'application_name': 'Tag+Pattern+Rec', 
              'oauth_consumer_key': consumer_key}
	oauthRequest = oauth.OAuthRequest.from_token_and_callback(token=requestToken, callback='', http_url='https://api-user.netflix.com/oauth/login', parameters=params)
	return (requestToken, oauthRequest.to_url())

	#return {'oauth_token':access_response_dict['oauth_token'][0], 'oauth_token_secret':access_response_dict['oauth_token_secret'][0], 'consumer_key':consumer_key,}
	#return {'oauth_token':token_response_dict['oauth_token'][0], 'oauth_token_secret':token_response_dict['oauth_token_secret'][0], 'consumer_key':consumer_key,}
	
def get_auth_access(auth_data):
	access_token_url = 'http://api.netflix.com/oauth/access_token'
	request_token = oauth.OAuthToken(auth_data['request']['key'], auth_data['request']['secret'])
	consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)
	access = oauth.OAuthRequest.from_consumer_and_token(consumer,  token = request_token, http_url=access_token_url)
	access.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), consumer, request_token)
	#access_token_url = 'http://api.netflix.com/oauth/access_token'
	#consumer = {'oauth_token': 'tcq3zq6ekxkzptucsybrkkaf', 'oauth_token_secret': 'VK2cVReGkB'}
	#token = {'oauth_token': token_response_dict['oauth_token'][0], 'oauth_token_secret':token_response_dict['oauth_token_secret'][0]}
	#request = OAuthRequest(access_token_url)
	#request.sign_request(OAuthSignatureMethod_HMAC_SHA1, consumer, token)
	access_url = access.to_url()
	print access_url
	temp_response = urllib.urlopen(access_url).read()
	accessToken = oauth.OAuthToken.from_string(temp_response)
	return accessToken
	#access_response_dict = urlparse.parse_qs(temp_response)
	#print access_response_dict
	#print "\n\n"
	#access_token_params = urllib.urlencode({'consumer_key':consumer_key, 'consumer_secret':consumer_secret, 'oauth_token':token_response_dict['oauth_token'], 'oauth_token_secret':token_response_dict['oauth_token_secret']})
	
def get_movie_details(auth_dict, movie_id):
	consumer = oauth.OAuthToken(consumer_key, consumer_secret)
	token = oauth.OAuthToken(auth_data['access']['key'], auth_data['access']['secret'])
	request = oauth.OAuthRequest.from_consumer_and_token(consumer,  http_url=movie_url + movie_id)
	request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), consumer, None)
	request_url = request.to_url()
	temp_response = urllib.urlopen(request_url).read()
	movie_details = temp_response
	doc = libxml2dom.parseString(movie_details)
	categories = doc.getElementsByTagName('category')
	cat_list = []
	for cat in categories:
		cat_list.append(cat.getAttribute("label"))
	return cat_list

#first_run = 0
#movie_get = 1	
#if first_run == 1:
#	print "\n\n\n"	
#	(requestToken, url) = get_auth_request()
#	file = open("authdat.txt","w")
#	file.write(str(requestToken.key)+'\t')
#	file.write(str(requestToken.secret)+'\t')
	#for key in requestToken:
		#file.write(str(requestToken[key])+'\t')
#	file.write('\n' + url)
#	file.close()
#elif movie_get == 0:
#	accessToken = get_auth_access(auth_data)
#	file = open("accdat.txt","w")
#	file.write(str(accessToken.key)+'\t')
#	file.write(str(accessToken.secret)+'\t')
#	file.close()
#else:
#	movie_details = get_movie_details(auth_data, test_id)
#	file = open("test_movie.txt","w")
#	file.write(movie_details)
#	file.close()

#movie_details = get_movie_details(auth_dict, test_id)
#print movie_details